package com.cbank.controller.admin;



import com.cbank.constants.Constants;
import com.cbank.constants.Result;
import com.cbank.constants.ResultCode;
import com.cbank.utils.QiniuUtils;
import com.cbank.utils.StringUtil;
import com.cbank.utils.UniqueKeyGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/file")
public class  FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> uploade(@RequestParam(value = "file", required = true) CommonsMultipartFile file) {
        String fileName = null;
        try {
            fileName = QiniuUtils.upload(file.getBytes(), UniqueKeyGenerator.generateUUID());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<String>(ResultCode.ERROR_QINIU.getCode(), ResultCode.SUCCESS.getMsg(), StringUtil.EMPTY_STRING);
        }
        return new Result<String>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), fileName);
    }


    @RequestMapping(value = "/ue/upload", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> uploadFile() {
        Map<String, Object> map = new HashMap<>();
        map.put("imageActionName", Constants.imageActionName);
        map.put("imageFieldName", Constants.imageFieldName);
        map.put("imageMaxSize", Constants.imageMaxSize);
        map.put("imageAllowFiles", Constants.imageAllowFiles);
        map.put("imageCompressEnable", Constants.imageCompressEnable);
        map.put("imageCompressBorder", Constants.imageCompressBorder);
        map.put("imageInsertAlign", Constants.imageInsertAlign);
        map.put("imageUrlPrefix", Constants.imageUrlPrefix);
        map.put("imagePathFormat", Constants.imagePathFormat);
        map.put("videoActionName",Constants.videoActionName);
        map.put("videoFieldName",Constants.videoFieldName);
        map.put("videoUrlPrefix",Constants.videoUrlPrefix);
        map.put("videoMaxSize",Constants.videoMaxSize);
        map.put("videoAllowFiles",Constants.videoAllowFiles);
        return map;
    }

    @RequestMapping(value = "/ue/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadFile(@RequestParam(value = "file") CommonsMultipartFile file) {
        String fileName = null;
        Map<String, Object> map = new HashMap<>();
        try {
            fileName = QiniuUtils.upload(file.getBytes(), UniqueKeyGenerator.generateUUID());
            map.put("state", "SUCCESS");
            map.put("url", fileName);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", "ERROR");
            map.put("url", "");
            return map;
        }
        return map;
    }

//    /**
//     * ue上传代码
//     *
//     * @param request
//     * @return
//     */
//    @RequestMapping(value = "/ue/uploadFile2", method = {RequestMethod.GET})
//    @ResponseBody
//    public String ueUploadFileGet(HttpServletRequest request, @RequestParam("callback")String callback) {
//        Map<String, Object> map = new HashMap<>();
//
//        map.put("imageActionName", Constants.imageActionName);
//        map.put("imageFieldName", Constants.imageFieldName);
//        map.put("imageMaxSize", Constants.imageMaxSize);
//        map.put("imageAllowFiles", Constants.imageAllowFiles);
//        map.put("imageCompressEnable", Constants.imageCompressEnable);
//        map.put("imageCompressBorder", Constants.imageCompressBorder);
//        map.put("imageInsertAlign", Constants.imageInsertAlign);
//        map.put("imageUrlPrefix", Constants.imageUrlPrefix);
//        map.put("imagePathFormat", Constants.imagePathFormat);
//        String t="{\"imageInsertAlign\":\"none\",\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imagePathFormat\":null,\"imageActionName\":\"none\",\"imageCompressEnable\":true,\"imageFieldName\":\"file\",\"imageCompressBorder\":1600,\"imageUrlPrefix\":null}";
//
//        return t;
//        //return map.toString();
//    }
//
//    /**
//     * ue上传代码
//     *
//     * @return
//     */
//    @RequestMapping(value = "/ue/uploadFile2", method = {RequestMethod.POST})
//    @ResponseBody
//    public Map<String, Object> ueUploadFile() {
//        return ueUploadFile();
//    }
//
//    /**
//     * ue上传代码
//     *
//     * @param request
//     * @return
//     */
//    @RequestMapping(value = "/ue/uploadFile2", method = {RequestMethod.POST})
//    @ResponseBody
//    public Map<String, Object> ueUploadFile(HttpServletRequest request) {
//        Map<String, Object> map = new HashMap<>();
//        try {
//            MultipartHttpServletRequest mulRequest = request instanceof MultipartHttpServletRequest
//                    ? (MultipartHttpServletRequest) request
//                    : null;
//            CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) mulRequest.getFile("file");
//
//            String fileName = QiniuUtils.upload(commonsMultipartFile.getBytes(), UniqueKeyGenerator.generateUUID());
//
//            map.put("url", fileName);
//            map.put("state", "SUCCESS");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(map.toString());
//
//        return map;
//    }



}
